//
//  RBInputBarView.m
//  RBDemoProj
//
//  Created by Lan on 2019/1/18.
//  Copyright © 2019年 SummerTea. All rights reserved.
//

#import "RBInputBarView.h"
#import "RBTextView.h"
#import "RBDefine.h"
#import <YYKeyboardManager.h>
#import "UIView+RBAdd.h"
#import <Masonry.h>
#import "RBThemeConfig.h"
#import "UIButton+RBAdd.h"

@interface RBInputBarView()<YYKeyboardObserver>

@property(nonatomic, strong) UIView *bar;
@property (strong, nonatomic) RBTextView *textView;
@property(nonatomic, strong) UIButton *confirmBtn;

@end

@implementation RBInputBarView

#pragma mark - System

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self setup];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder])
    {
        [self setup];
    }
    return self;
}

- (void)dealloc
{
    [[YYKeyboardManager defaultManager] removeObserver:self];
}

#pragma mark - Private

- (void)setup
{
    [[YYKeyboardManager defaultManager] addObserver:self];
    self.userInteractionEnabled = NO;
    self.hidden = YES;
//    self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
    self.maxHeight = 100;
    self.placeHolder = @"请输入内容";
    self.textView.hidden = NO;
}

#pragma mark - Public

- (void)clearInputBox
{
    self.textView.text = nil;
}

- (void)show
{
    [self.superview bringSubviewToFront:self];
    self.hidden = NO;
    self.userInteractionEnabled = YES;
    self.textView.placeHolder = self.placeHolder;
    if (self.text)
    {
        self.textView.text = self.text;
    }
    self.textView.maxTextLength = self.maxLength;
    [self.textView enableAutoGrowWithConsWidth:kRBMainScreenWidth - 82 minHeight:30 maxHeight:self.maxHeight - 10];
    [self.textView becomeFirstResponder];
}

- (void)dismiss
{
    self.hidden = YES;
    self.userInteractionEnabled = NO;
    [self.textView resignFirstResponder];
}

#pragma mark - Event

- (void)sendBtnTapped
{
    if (self.returnBlock)
    {
        self.returnBlock(self.textView.text);
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    CGPoint point = [[touches anyObject] locationInView:self];
    if (!CGRectContainsPoint(self.bar.frame, point))
    {
        [self dismiss];
    }
}

- (void)barTapped
{
    
}

#pragma mark - YYKeyboardObserver

- (void)keyboardChangedWithTransition:(YYKeyboardTransition)transition
{
    CGRect kbFrame = [[YYKeyboardManager defaultManager] convertRect:transition.toFrame toView:self.superview];
    [UIView animateWithDuration:transition.animationDuration animations:^{
        [self.bar mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(transition.fromVisible? self.maxHeight: -kbFrame.size.height);
        }];
        [self.bar.superview layoutIfNeeded];
    }];
}

#pragma mark - Setter

- (void)setText:(NSString *)text
{
    self.textView.text = text;
}

#pragma mark - Getter

- (NSString *)text
{
    return self.textView.text;
}

- (UIView *)bar
{
    if (!_bar)
    {
        self.bar = ({
            UIView *view = [UIView new];
            [view rbAddTapGesWithTarget:self action:@selector(barTapped)];
            view.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
            [self addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.mas_equalTo(0);
                make.bottom.mas_equalTo(-100);
            }];
            view;
        });
    }
    return _bar;
}

- (UIButton *)confirmBtn
{
    if (!_confirmBtn)
    {
        self.confirmBtn = ({
            UIButton *btn = [UIButton new];
            btn.showsTouchWhenHighlighted = YES;
            btn.backgroundColor = [RBThemeConfig themeColor];
            [btn rbSetFontSize:14];
            [btn rbSetTitleColor:[UIColor whiteColor]];
            [btn rbSetTitle:@"发送"];
            [btn rbSetCornerRadius:3];
            [btn rbAddTarget:self action:@selector(sendBtnTapped)];
            [self.bar addSubview:btn];
            [btn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.width.mas_equalTo(46);
                make.height.mas_equalTo(30);
                make.bottom.mas_equalTo(-5);
                make.right.mas_equalTo(-12);
            }];
            btn;
        });
    }
    return _confirmBtn;
}

- (RBTextView *)textView
{
    if (!_textView)
    {
        self.textView = ({
            RBTextView *view = [RBTextView new];
            view.font = [UIFont systemFontOfSize:14];
            view.placeHolderFontSize = 14;
            [self.bar addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(12);
                make.right.mas_equalTo(self.confirmBtn.mas_left).offset(-12);
                make.height.mas_equalTo(30);
                make.top.mas_equalTo(5);
                make.bottom.mas_equalTo(-5);
            }];
            view;
        });
    }
    return _textView;
}

@end
